<head>
    <meta charset="utf-8" /> 
</head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

<button id="btnGo" onclick="run('go')">运行GO</button><button id="btnMs" onclick="run('ms')">运行dotnet</button>
<pre id="result"></pre>

<script>
document.onreadystatechange = () =>
{
    if(location.href.indexOf(":12345") > -1)
        document.getElementById("btnMs").style.display = "none"
    else if(location.href.indexOf(":54321") > -1)
        document.getElementById("btnGo").style.display = "none"
}

var server = location.href.indexOf("file://") > -1? "localhost" : location.href.split(/\/|\:/).filter(a => a != "")[1]
console.log(server)

function run(rn)
{
    var port = rn == "go"? 12345 : 54321

    if(location.href.indexOf("/test/") < 0)
    {
        location.href = `http://${server}:${port}/test/`
        return
    }

    var content = []
    var total = 3000, requested = 0
    
    var test = "josdfjataahf9jf34584+r52fa09ew8if92i390fi dfasadfsjf234t 2k34%^@#$k0f-kasdfjk09-K"
    var standard = 0
    for(var i = 0; i < total; i++)
    {
        var t = test.substr(i % test.length)
        if(t == "") continue

        for(var s = 0, j = t.length - 1; j > -1; j--) 
            s += t[j].charCodeAt()
        standard += s * t.length
    }

    var chk = 0;
    (function check()
    {
        $("#result").html(`第${++chk}轮检测：共${total}个请求，发送了${requested}个，回应了${content.length}个。`)

        if(content.length < total)
        {
            setTimeout(check, 1000)
            return
        }

        content.sort((a, b) => a[1] - a[0] > b[1] - b[0]? 1 : -1)
        var consume = 0, result = 0
        content.forEach(v =>
        {
            consume += v[1] - v[0]
            result += parseInt(v[2])
        })
        $("#result").html(`最小：${parseInt(content[0][1] - content[0][0])} 毫秒    最大：${parseInt(content[content.length - 1][1] - content[content.length - 1][0])} 毫秒      平均：${parseInt(consume / content.length)} 毫秒<br>标准答案：${standard}<br>测试答案：${result}`)
    })()

    var tested = []
    for(var i = 0; i < total; i++) tested.push(test.substr(i % test.length))

    for(var requested = 0; requested < total; requested++)
        (req => setTimeout(() =>
                            {
                                var start = performance.now()

                                $.post(`http://${server}:${port}/test/jasdoif?r=1`,
                                        rn == "go"? {data:tested[req]} : tested[req],
                                        function(result)
                                        {
                                            content.push([start, performance.now(), result])
                                        })
                            }, requested >> 3)
        )(requested)
}
</script>